#include<iostream>
#include <utility>

using namespace std;

template<class K,class V>
class AVLTree
{
private:
	class TreeNode
	{
	public:
		pair<K, V> _kv;

		TreeNode* _left=nullptr;
		TreeNode* _right=nullptr;
		TreeNode* _parent=nullptr;

		int _bf=0;
		TreeNode(const pair<K,V>& kv)
			:_kv(kv)
		{}
		TreeNode(const pair<K, V>& kv,int bf)
			:_kv(kv)
			,_bf(bf)
		{}
	};
	//拷贝函数子函数
	TreeNode* _Copy(TreeNode* root)
	{
		if (root == nullptr)return nullptr;
		TreeNode* newnode = new TreeNode(root->_kv, root->_bf);
		newnode->_left = _Copy(root->_left);
		newnode->_right = _Copy(root->_right);
		if (newnode->_left)
		{
			newnode->_left->_prante = newnode;
		}
		if (newnode->_right)
		{
			newnode->_right->_prante = newnode;
		}
		return newnode;
	}
	//清空函数
	void _Clean(TreeNode* root)
	{
		if (root == nullptr)return;
		_Clean(root->_left);
		_Clean(root->_right);
		delete root;
	}
	//中序遍历子函数
	void _InOrder(TreeNode* root)
	{
		if (root == nullptr)return;
		_InOrder(root->_left);
		cout << root->_kv.first<<" ";
		_InOrder(root->_right);
	}
private:
	void RotateR(TreeNode* parent)
	{
		TreeNode* subL = parent->_left;
		TreeNode* subLR = subL->_right;
		TreeNode* parentParent = parent->_parent;

		//建立subL和parent的映射
		subL->_right = parent;
		parent->_parent = subL;
		
		//建立parent和subLR的映射
		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		//建立subL和parentParent的映射
		if (parentParent == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subL;
			}
			else
			{
				parentParent->_right = subL;
			}
			subL->_parent = parentParent;
		}
		subL->_bf = parent->_bf = 0;
	}
	void RotateL(TreeNode* parent)
	{
		TreeNode* subR = parent->_right;
		TreeNode* subRL = subR->_left;
		TreeNode* parentParent = parent->_parent;

		//建立subR和parent的映射
		subR->_left = parent;
		parent->_parent = subR;

		//建立parent与subRL的映射
		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		//建立subR与parentParent的映射
		if (parentParent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}
			subR->_parent = parentParent;
		}
		subR->_bf = parent->_bf = 0;
	}
	void RotateLR(TreeNode* parent)
	{
		TreeNode* subL = parent->_left;
		TreeNode* subLR = subL->_right;
		int bf = subLR->_bf;

		RotateL(subL);
		RotateR(parent);

		if (bf == -1)
		{
			parent->_bf = 1;
			subL->_bf = 0;
			subLR->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = 0;
			subL->_bf = -1;
			subLR->_bf = 0;
		}
		else if (bf == 0)
		{
			parent->_bf = 0;
			subL->_bf = 0;
			subLR->_bf = 0;
		}


	}
	void RotateRL(TreeNode* parent)
	{
		TreeNode* subR = parent->_right;
		TreeNode* subRL = subR->_left;
		int bf = subRL->_bf;

		RotateR(subR);
		RotateL(parent);

		if (bf == 1)
		{
			parent->_bf = -1;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 0;
			subR->_bf = 1;
			subRL->_bf = 0;
		}
		else if (bf == 0)
		{
			parent->_bf = 0;
			subR->_bf = 0;
			subRL->_bf = 0;
		}

	}
public:

	//构造函数
	AVLTree()
	{
		_root = nullptr;
	}

	//拷贝构造函数
	AVLTree(const AVLTree<K,V>& tree)
	{
		_root = _Copy(tree._root);
	}

	//赋值运算符重载函数
	AVLTree<K,V>& operator=(AVLTree<K, V> tree)
	{
		if (tree._root != nullptr)
		{
			_Clean(_root);
			_root = _Copy(tree.root);
		}
		return *this;
	}

	//析构函数
	~AVLTree()
	{
		if (_root != nullptr)
		{
			_Clean(_root);
		}
	}

	//插入函数
	bool Insert(const pair<K,V>& kv)
	{
		if (_root == nullptr)
		{
			TreeNode* newnode = new TreeNode(kv);
			_root = newnode;
			return true;
		}
		int key = kv.first;
		TreeNode* cur = _root;
		TreeNode* parent = nullptr;
		while (cur)
		{
			if (key > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (key < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new TreeNode(kv);
		if (key>parent->_kv.first)
		{
			parent->_right = cur;
			cur->_parent = parent;
		}
		else
		{
			parent->_left = cur;
			cur->_parent = parent;
		}
		while (cur != _root)
		{
			if (cur == parent->_left)
			{
				parent->_bf--;
			}
			else if (cur == parent->_right)
			{
				parent->_bf++;
			}
			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf == -1 || parent->_bf == 1)
			{
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == -2 || parent->_bf == 2)
			{
				if (parent->_bf == -2)
				{
					if (cur->_bf == -1)
					{
						RotateR(parent);
					}
					else
					{
						RotateLR(parent);
					}
				}
				else if(parent->_bf == 2)
				{
					if (cur->_bf == -1)
					{
						RotateRL(parent);
					}
					else
					{
						RotateL(parent);
					}
				}
				break;
			}
			else
			{
				cout << "pingheng" << endl;
			}
		}
		return true;
	}

	//删除函数
	bool Erase(const K& key)
	{
		if (_root == nullptr)return false;
		TreeNode* cur = _root;
		TreeNode* parent = _root;

		//用于标记实际的待删除结点及其父结点
		TreeNode* delParentPos = nullptr;
		TreeNode* delPos = nullptr;
		while (cur)
		{
			if (key > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (key < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				if (cur->_left == nullptr)
				{
					if (cur == _root)
					{
						_root = _root->_right;
						if (_root)
							_root->_parent = nullptr;
						delete cur;
						return true;
					}
					else
					{
						delParentPos = parent; 
						delPos = cur;
					}
					break;
				}
				else if (cur->_right == nullptr)
				{
					if (cur == _root)
					{
						_root = _root->_left;
						if (_root)
							_root->_parent = nullptr;
						delete cur;
						return true;
					}
					else
					{
						delParentPos = parent;
						delPos = cur;
					}
					break;
				}
				else
				{
					TreeNode* minright = cur->_right;
					TreeNode* minparent = cur;
					while (minright->_left)
					{
						minparent = minright;
						minright = minright->_left;
					}
					cur->_kv.first = minright->_kv.first;
					cur->_kv.second = minright->_kv.second;
					delParentPos = minparent;
					delPos = minright;
					break;
				}
			}
		}
		if (delParentPos == nullptr)
		{
			return false;
		}

		//记录待删除结点及其父结点（用于后续实际删除）
		TreeNode* del = delPos;
		TreeNode* delP = delParentPos;

		while (delPos != _root)
		{
			if (delPos == delParentPos->_left)
			{
				delParentPos->_bf++;
			}
			else if (delPos == delParentPos->_right)
			{
				delParentPos->_bf--;
			}
			if (delParentPos->_bf == -1 || delParentPos->_bf == 1)
			{
				break;
			}
			else if (delParentPos->_bf == 0)
			{
				delPos = delParentPos;
				delParentPos = delParentPos->_parent;
			}
			else if (delParentPos->_bf == -2 || delParentPos->_bf == 2)
			{
				if (delParentPos->_bf == -2)
				{
					if (delParentPos->_left->_bf == -1)
					{
						TreeNode* tmp = delParentPos->_left;
						RotateR(delParentPos);
						delParentPos = tmp;
					}
					else if (delParentPos->_left->_bf == 1)
					{
						TreeNode* tmp = delParentPos->_left->_right;
						RotateLR(delParentPos);
						delParentPos = tmp;
					}
					else 
					{
						TreeNode* tmp = delParentPos->_left;
						RotateR(delParentPos);
						delParentPos = tmp;

						delParentPos->_bf = 1;
						delParentPos->_right->_bf = -1;
						break;
					}
					
				}
				else
				{
					if (delParentPos->_right->_bf == -1)
					{
						TreeNode* tmp = delParentPos->_right->_left;
						RotateRL(delParentPos);
						delParentPos = tmp;
					}
					else if (delParentPos->_right->_bf == 1)
					{
						TreeNode* tmp = delParentPos->_right;
						RotateL(delParentPos);
						delParentPos = tmp;
					}
					else
					{
						TreeNode* tmp = delParentPos->_right;
						RotateL(delParentPos);
						delParentPos = tmp;

						delParentPos->_bf = -1;
						delParentPos->_left->_bf = 1;
						break;
					}
				}
				delPos = delParentPos;
				delParentPos = delParentPos->_parent;
			}
		}
		//进行实际删除
		if (del->_left == nullptr) //实际删除结点的左子树为空
		{
			if (del == delP->_left) //实际删除结点是其父结点的左孩子
			{
				delP->_left = del->_right;
				if (del->_right)
					del->_right->_parent = delP;
			}
			else //实际删除结点是其父结点的右孩子
			{
				delP->_right = del->_right;
				if (del->_right)
					del->_right->_parent = delP;
			}
		}
		else //实际删除结点的右子树为空
		{
			if (del == delP->_left) //实际删除结点是其父结点的左孩子
			{
				delP->_left = del->_left;
				if (del->_left)
					del->_left->_parent = delP;
			}
			else //实际删除结点是其父结点的右孩子
			{
				delP->_right = del->_left;
				if (del->_left)
					del->_left->_parent = delP;
			}
		}
		delete del; //实际删除结点
		return true;
	}

	//查找函数
	TreeNode* Find(const K& value)
	{
		if (_root == nullptr)return nullptr;
		TreeNode* cur = _root;
		while (cur)
		{
			if (value > cur->_kv.first)
			{
				cur = cur->_right;
			}
			else if (value < cur->_kv.first)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}

	//中序遍历
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	
private:
	TreeNode* _root;
};
int main()
{
	AVLTree<int,int> t;
	t.Insert(make_pair(6,0));
	t.Insert(make_pair(3, 0));
	t.Insert(make_pair(2, 0));
	t.Insert(make_pair(1, 0));
	t.Insert(make_pair(7, 0));
	t.Insert(make_pair(8, 0));
	t.Insert(make_pair(4, 0));
	t.Insert(make_pair(5, 0));
	t.Insert(make_pair(9, 0));
	t.InOrder();

	t.Erase(6);
	t.InOrder();

	t.Erase(3);
	t.InOrder();

	t.Erase(2);
	t.InOrder();

	t.Erase(1);
	t.InOrder();

	t.Erase(7);
	t.InOrder();

	t.Erase(8);
	t.InOrder();

	t.Erase(4);
	t.InOrder();

	t.Erase(5);
	t.InOrder();

	t.Erase(9);
	t.InOrder();


	return 0;
}